//
//  NetworkTools.swift
//  Weibo
//
//  Created by 张远文 on 16/4/30.
//  Copyright © 2016年 张远文. All rights reserved.
//

import UIKit
import AFNetworking

private let weiboDomain = "com.baidu.data.error"

//线上
private let hostname = "https://api.weibo.com/"

//测试
//private let hostname = "https://test.weibo.com/"

enum HTTPMethod: String {
    case POST = "POST"
    case GET = "GET"
}

class NetworkTools: AFHTTPSessionManager {

    //定义单例对象
    static let sharedTools: NetworkTools = {
       
        let url = NSURL(string: hostname)
        let tools = NetworkTools(baseURL: url)
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
    
    //定义网络访问方法
    
    //加载字典数据
    func requestJsonDict(method: HTTPMethod, urlString: String, parmaters: [String : AnyObject]?, finished:(result: [String : AnyObject]?, error: NSError?) -> ()) {
        
        if method == HTTPMethod.GET {
            
            GET(urlString, parameters: parmaters, progress: nil, success: { (_, result) in
                
                //检测数据是否是字典
                if let dict = result as? [String : AnyObject] {
                    
                    finished(result: dict, error: nil)
                    return
                }
                
                //数据格式不匹配
                //自定义错误信息
                //一般定义errorcode 为负值
                let dataError = NSError(domain: weiboDomain, code: -10000, userInfo: [NSLocalizedDescriptionKey : "数据错误"])
                finished(result: nil, error: dataError)
                
                }, failure: { (_, error) in
                    
                    finished(result: nil, error: error)
                    print(error)
            })
            
        } else {
           
            POST(urlString, parameters: parmaters, progress: nil, success: { (_, result) in
                
                if let dict = result as? [String : AnyObject] {
                    
                    finished(result: dict, error: nil)
                    return
                }
                
                let dataError = NSError(domain: weiboDomain, code: -10000, userInfo: [NSLocalizedDescriptionKey : "数据错误"])
                finished(result: nil, error: dataError)
                
                }, failure: { (_, error) in
                    
                    finished(result: nil, error: error)
                    print(error)
            })
            
        }
    }
    
    //添加上传图片方法
    func uploadImage(urlString: String, parmaters: [String : AnyObject], imageData: NSData, finished: (result: [String : AnyObject]?, error: NSError?) -> ()) {
        
        POST(urlString, parameters: parmaters, constructingBodyWithBlock: { (multipartFormData) in
            
            /**
             *  1. data 要上传的文件的二进制数据
             *  2. name 表示上传的文件服务器对应的字段
             *  3. filename 表示服务器接收后 存储的名字 名字可以随意
             *  4. mimeType 上传的文件格式 image/jpeg
             */
            //AFMultipartFormData
            //multipartFormData.appendPartWithFormData(imageData, name: "pic")
            multipartFormData.appendPartWithFileData(imageData, name: "pic", fileName: "OBJ", mimeType: "image/jpeg")
            
            }, progress: nil, success: { (_, result) in
                
                if let dict = result as? [String : AnyObject] {
                    print("发布图文微博网络请求成功")
                    finished(result: dict, error: nil)
                }
                
            }) { (_, error) in
                finished(result: nil, error: error)
                print(error)
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
